package weaver.interfaces.hitech.duss.action.wlbmsq;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-06-17
 * @Description: ${description}
 * @ModifyLog:
 **/

public class WeaWlbmsqAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        String curDate = requestInfo.getRequestManager().getCurrentDate().replaceAll("-", "");
        baseBean.writeLog("WeaWlbmsqAction--requestID:" + requestid);
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        try {
            //主表
            rs.executeQuery("select id,lb,gc from " + tablename + " where requestid=?", requestid);
            baseBean.writeLog("select id,lb,gc from " + tablename + " where requestid=" + requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));//外键
            String lb = Util.null2String(rs.getString("lb"));//类别 0-备件维修 1-外料其他
            String gc = Util.null2String(rs.getString("gc"));//工厂
            if (gc.equals("0")) {//封装
                gc = "1000";
            } else if (gc.equals("1")) {
                gc = "2000";
            }
            if ("0".equals(lb)) {//主表类别为备件维修，取明细表1和主表字段
                //明细1
                rs1.executeQuery("select id,codeqz,mtpy,ms,dw,mg,pg,modelno,aqkc,zwpm,mrpcontroller,valuationclass from " + tablename + "_dt1 where mainid=?", mainid);
                while (rs1.next()) {
                    Map<String, String> parmaMap = new HashMap<>();
                    String id = Util.null2String(rs1.getString("id"));//id
                    String codeqz = Util.null2String(rs1.getString("codeqz"));//Code前缀
                    String mtpy = Util.null2String(rs1.getString("mtpy"));//MTPY
                    if ("0".equals(mtpy)) {
                        mtpy = "ERSA";
                    } else if ("1".equals(mtpy)) {
                        mtpy = "ROH";
                    } else if ("2".equals(mtpy)) {
                        mtpy = "FHMI";
                    } else if ("3".equals(mtpy)) {
                        mtpy = "HIBE";
                    }
                    String ms = Util.null2String(rs1.getString("ms"));//描述
                    String dw = Util.null2String(rs1.getString("dw"));//单位
                    String mg = Util.null2String(rs1.getString("mg"));//Matl Group
                    String pg = Util.null2String(rs1.getString("pg"));//Purchase Group
                    String modelno = Util.null2String(rs1.getString("modelno").toUpperCase());//MODEL NO
                    String aqkc = Util.null2String(rs1.getString("aqkc"));//安全库存
                    String zwpm = Util.null2String(rs1.getString("zwpm"));//中文品名
                    String mrpcontroller = Util.null2String(rs1.getString("mrpcontroller"));//mrpcontroller
                    String valuationclass = Util.null2String(rs1.getString("valuationclass"));//valuationclass
                    parmaMap.put("WERKS", gc);
                    parmaMap.put("ZTXT04", codeqz);
                    parmaMap.put("MTART", mtpy);
                    parmaMap.put("MAKTX", ms);
                    parmaMap.put("MEINS", dw);
                    parmaMap.put("MATKL", mg);
                    parmaMap.put("EKGRP", pg);
                    parmaMap.put("MFRPN", modelno);
                    parmaMap.put("EISBE", aqkc);
                    parmaMap.put("MODEL", modelno);
                    parmaMap.put("DISPO", mrpcontroller);
                    parmaMap.put("BKLAS", valuationclass);
                    baseBean.writeLog("parmaMap:" + parmaMap.toString());
                    ZIF_MATERIAL_CREATE create = new ZIF_MATERIAL_CREATE();//调用函数
                    Map resultMap = create.execute(parmaMap, curDate);
                    String E_MATNR = (String) resultMap.get("E_MATNR");
                    String E_MSG = (String) resultMap.get("E_MSG");
                    String E_CHK_TYPE = (String) resultMap.get("E_CHK_TYPE");
                    baseBean.writeLog("SAP返回结果——E_MATNR=" + E_MATNR + ";E_MSG=" + E_MSG + ";E_CHK_TYPE=" + E_CHK_TYPE);
                    if ("S".equalsIgnoreCase(E_CHK_TYPE)) {
                        rs1.executeUpdate("update " + tablename + "_dt1 set sapcdoe='" + E_MATNR + "',emsg='" + E_MSG + "',echktype='" + E_CHK_TYPE + "'  where mainid='" + mainid + "' and id='"+id+"'");
                        //SAPCODE返回后，MTPY值为ERSA的或FHMI类型的以FCA或Q开头的物料需要调用以下接口ZRFC_CREATE_COMM_CODE
                        if (("ERSA".equals(mtpy)) || ("Q".equals(mtpy.substring(0, 1))) || ("FCA".equals(mtpy.substring(0, 3)))) {
                            ZRFC_CREATE_COMM_CODE code = new ZRFC_CREATE_COMM_CODE();
                            Map<String, String> map = new HashMap();
                            map.put("I_MATNR", E_MATNR);
                            map.put("I_PLANT", gc);
                            map.put("I_CHNAME", zwpm);
                            baseBean.writeLog("map:" + map.toString());
                            code.execute(map);
                        }
                    } else {
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(E_MSG);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            } else {//主表类别为外料其他，取明细表2和主表字段
                //明细2
                rs1.executeQuery("select id,codeqz,mtpy,ms,dw,matlgroup,purchasegroup,modelno,mrpcontroller,valuationclass,zwpm,aqkc from " + tablename + "_dt2 where mainid=?", mainid);
                while (rs1.next()) {
                    Map<String, String> map1 = new HashMap<>();
                    String id = Util.null2String(rs1.getString("id"));//id
                    String codeqz = Util.null2String(rs1.getString("codeqz"));//Code前缀
                    String mtpy = Util.null2String(rs1.getString("mtpy"));//MTPY
                    if ("0".equals(mtpy)) {
                        mtpy = "ERSA";
                    } else if ("1".equals(mtpy)) {
                        mtpy = "ROH";
                    } else if ("2".equals(mtpy)) {
                        mtpy = "FHMI";
                    } else if ("3".equals(mtpy)) {
                        mtpy = "HIBE";
                    }
                    String ms = Util.null2String(rs1.getString("ms"));//描述
                    String dw = Util.null2String(rs1.getString("dw"));//单位
                    String aqkc = Util.null2String(rs1.getString("aqkc"));//aqkc
                    String matlgroup = Util.null2String(rs1.getString("matlgroup"));//Matl Group
                    String purchasegroup = Util.null2String(rs1.getString("purchasegroup"));//Purchase Group
                    String modelno = Util.null2String(rs1.getString("modelno").toUpperCase());//MODEL NO
                    String mrpcontroller = Util.null2String(rs1.getString("mrpcontroller"));//MRP CONTROLLER
                    String valuationclass = Util.null2String(rs1.getString("valuationclass"));//Valuation Class
                    String zwpm = Util.null2String(rs1.getString("zwpm"));//中文品名
                    map1.put("WERKS", gc);
                    map1.put("ZTXT04", codeqz);
                    map1.put("MTART", mtpy);
                    map1.put("MAKTX", ms);
                    map1.put("MEINS", dw);
                    map1.put("MATKL", matlgroup);
                    map1.put("EKGRP", purchasegroup);
                    map1.put("MFRPN", modelno);
                    map1.put("DISPO", mrpcontroller);
                    map1.put("EISBE", aqkc);
                    map1.put("BKLAS", valuationclass);
                    map1.put("MODEL", modelno);

                    baseBean.writeLog("map1:" + map1.toString());
                    ZIF_MATERIAL_CREATE create = new ZIF_MATERIAL_CREATE();
                    Map resultMap = create.execute(map1, curDate);
                    String E_MATNR = (String) resultMap.get("E_MATNR");
                    String E_MSG = (String) resultMap.get("E_MSG");
                    String E_CHK_TYPE = (String) resultMap.get("E_CHK_TYPE");
                    baseBean.writeLog("SAP返回结果——E_MATNR=" + E_MATNR + ";E_MSG=" + E_MSG + ";E_CHK_TYPE=" + E_CHK_TYPE);
                    if ("S".equalsIgnoreCase(E_CHK_TYPE)) {
                        rs1.executeUpdate("update " + tablename + "_dt2 set sapcdoe='" + E_MATNR + "',emsg='" + E_MSG + "',echktype='" + E_CHK_TYPE + "' where mainid='" + mainid + "' and id='"+id+"'");
                        //SAPCODE返回后，MTPY值为ERSA的或FHMI类型的以FCA或Q开头的物料需要调用以下接口ZRFC_CREATE_COMM_CODE
                        if (("ERSA".equals(mtpy)) || ("Q".equals(mtpy.substring(0, 1))) || ("FCA".equals(mtpy.substring(0, 3)))) {
                            ZRFC_CREATE_COMM_CODE code = new ZRFC_CREATE_COMM_CODE();
                            Map<String, String> map = new HashMap();
                            map.put("I_MATNR", E_MATNR);
                            map.put("I_PLANT", gc);
                            map.put("I_CHNAME", zwpm);
                            baseBean.writeLog("map:" + map.toString());
                            code.execute(map);
                        }
                    } else {
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent("SAP返回报错信息：" + E_MSG);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseBean.writeLog("异常参考:" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
